# -*- coding: utf-8 -*-
"""
@Version : 
@Author  : ChenMZ
@Time    : 2025/09/19
@File    : cpu_dev.py
@Desc    : 
@Modify  : 
"""
import wmi
import GPUtil
import psutil
import subprocess
from typing import Dict, Union, List

from device._dev_model import Dev, DevGroup
from device.dev_base import CpuBase
from device.dev_entity.cpu_entity import CpuEntity


class CPU(Dev):
    def __init__(self):
        self._abc_ = CpuBase()
        self._base_: CpuEntity = self._abc_[0]
        self._idx_ = 0

    @property
    def dev_idx(self):
        return self._idx_

    @dev_idx.setter
    def dev_idx(self, value: int):
        if value < len(self._abc_):
            self._idx_ = value
            self._base_ = self._abc_[self._idx_]
        else:
            raise ValueError("Out of range")

    @property
    def dev_list(self):
        if isinstance(self._base_, CpuBase):
            return self._base_.to_list()
        else:
            return [self._base_]

    def get_dev_name(self) -> str:
        # return platform.processor()
        # 2025.9.22 platform.processor() 无法返回与任务管理器相同的处理器名称, 变更为以下方法
        # from win32com.client import GetObject
        # root_winmgmts = GetObject("winmgmts:root\cimv2")
        # cpus = root_winmgmts.ExecQuery("Select * from Win32_Processor")
        # return cpus[0].Name.strip()
        return self._base_.cpu_name

    @property
    def core_num(self):
        return self._base_.number_of_cores

    @property
    def process_num(self):
        return self._base_.number_of_logical_processors

    @property
    def max_freq(self):
        return self._base_.max_clock_speed

    @property
    def l2_cache(self):
        return self._base_.l2_cache_size, self._base_.l2_cache_speed

    @property
    def l3_cache(self):
        return self._base_.l3_cache_size, self._base_.l3_cache_speed

    @property
    def get_current_freq(self):
        return psutil.cpu_freq().current

    @property
    def get_temp(self):
        return psutil.sensors_temperatures()

    def get_detail_info(self) -> Dict[str, Union[str, bool, float, int]]:
        return self._base_.to_dict()

    def refresh(self):
        return self._base_.recent_percentage

    def get_usage(self):
        return psutil.cpu_percent(interval=0.5)
        # return self._base_.load_percentage


class GPUs(DevGroup):
    def __init__(self):
        info = {}

        c = wmi.WMI()
        for gpu in c.Win32_VideoController():
            if "Intel" in gpu.Name or "AMD" in gpu.Name or "集成" in gpu.Name:
                info['usage'] = "需要专用工具获取实时使用率"
                break
        self._dev_list_: List[GPU] = []
        for gpu in GPUtil.getGPUs():
            self._dev_list_.append(GPU(gpu))

    def get_dev(self) -> List[Dev]:
        return self._dev_list_


class GPU(Dev):

    def __init__(self, gpu: GPUtil.GPU):
        self._gpus: GPUtil.GPU = gpu

    def get_dev_name(self) -> str:
        return self._gpus.name

    def get_detail_info(self) -> Dict[str, Union[str, bool, float, int]]:
        info = {
            "UUID": self._gpus.uuid,
            "序列号": self._gpus.serial,
            "显存": self._gpus.memoryTotal,
            "驱动版本": self._gpus.driver,
            "是否链接到显示器": self._gpus.display_mode,
        }
        return info

    def refresh(self):
        pass

    def get_usage(self):
        pass


class ETHs(DevGroup):
    def get_dev(self) -> List[Dev]:
        pass


class ETH(Dev):
    def get_dev_name(self) -> str:
        pass

    def get_detail_info(self) -> Dict[str, Union[str, bool, float, int]]:
        pass

    def refresh(self):
        pass

    def get_usage(self):
        pass


if __name__ == '__main__':
    import cProfile
    test = CPU()
    cProfile.run("test.get_dev_name()", sort="cumtime")
